package com.奇技淫巧;

import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * 短信服务
 */
public class SmsService {

    public static void main(String[] args) {

        new SmsService().send();
    }

    /**
     * 其他短信服务商，支持自定义模版
     */
    public void send(){
        //String phone ,String smsmode,String content
        String accountId="dlhaih00";
        String password="hnhh20211011";
        Integer ProductId=1012818;

        String phone = "18867363985";

        Long random = 246789L;
        Long timestamp = System.currentTimeMillis()/1000;

        JSONObject body = new JSONObject();
        body.put("AccountId", accountId);
        //body.put("ExtendNo","");
        body.put("ProductId", ProductId);
        body.put("PhoneNos", phone);
        body.put("Content", "【我是测试】-长沙公安温馨提醒，你的账号存在安全风险，请及时更新账号密码");
        body.put("AccessKey", calcSendSmsSign(accountId, phone, password,random,timestamp));
        body.put("Timestamp", timestamp);
        body.put("Random", random);
        //body.put("OutId", "");
        //body.put("SendTime", "");
        JSONObject response = HttpUtils.sendPost("http://api.51welink.com/EncryptionSubmit/SendSms.ashx",null,body);
        System.out.println(response);

    }
    public static String calcSendSmsSign(String accountId, String mobile, String password, Long random,Long timestamp) {
        StringBuilder builder = new StringBuilder("AccountId=")
                .append(accountId)
                .append("&PhoneNos=")
                .append(mobile)
                .append("&Password=")
                //.append(Md5Utils.hash(password))
                .append(md5(password))
                .append("&Random=")
                .append(random)
                .append("&Timestamp=")
                .append(timestamp);
        return DigestUtils.sha256Hex(builder.toString());
    }

    static String md5(String password) {
        String encodeStr = DigestUtils.md5Hex(password + "SMmsEncryptKey");
        return encodeStr.toUpperCase();
    }


}
